<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>UDT Reference</title>
</head>

<body>

<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="100%" id="table1" bgcolor="#99CCFF" bordercolor="#99CCFF">
  <tr>
    <td width="100%"><font face="Verdana" size="2"><i>&nbsp;UDT </i></font><i>
	<font face="Verdana" size="2">Tutorial</font></i></td>
  </tr>
</table>
<h1><font face="Verdana" size="4">Hello World!</font></h1>
<p><font face="Verdana" size="2">In this section we will introduce the simplest 
UDT program that can transfer data in high performance.</font></p>
<p><font face="Verdana" size="2">This simple &quot;Hello World!&quot; example includes a 
server program and a client program just like any socket programming tutorial. 
These are the simpler version of the <font color="#000080">appserver</font> and
<font color="#000080">appclient</font> examples in <i>./app</i> directory. </font></p>
<p><font face="Verdana" size="2">To compile, use <font color="#800000">gcc -o 
server server.cpp -I&lt;udt.h location&gt; -L&lt;libudt.so location&gt; -ludt -lstdc++ -lpthread</font>. 
For more details, please refer to the <font color="#000080">Makefile</font> in .<i>/app</i> 
directory.</font></p>
<p><b><font face="Verdana" size="2">UDT server example</font></b></p>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="100%" id="table2" bgcolor="#C0C0C0" bordercolor="#C0C0C0">
  <tr>
    <td width="100%">
    <p style="margin-top: 0; margin-bottom: 0">
	<font face="Courier New" size="1">#include &lt;iostream&gt;<br>
	#include &lt;udt.h&gt;<br>
	#include &lt;arpa/inet.h&gt;<br>
	<br>
	using namespace std;<br>
	<br>
	using namespace UDT;<br>
	<br>
	int main()<br>
	{<br>
	UDTSOCKET serv = UDT::socket(AF_INET, SOCK_STREAM, 0);<br>
	<br>
	sockaddr_in my_addr;<br>
	my_addr.sin_family = AF_INET;<br>
	my_addr.sin_port = htons(9000);<br>
	my_addr.sin_addr.s_addr = INADDR_ANY;<br>
	memset(&amp;(my_addr.sin_zero), '\0', 8);<br>
	<br>
	if (UDT::ERROR == UDT::bind(serv, (sockaddr*)&amp;my_addr, sizeof(my_addr)))<br>
	{<br>
&nbsp;&nbsp; cout &lt;&lt; &quot;bind: &quot; &lt;&lt; UDT::getlasterror().getErrorMessage();<br>
&nbsp;&nbsp; return 0;<br>
	}<br>
	<br>
	CUDT::listen(serv, 10);<br>
	<br>
	int namelen;<br>
	sockaddr_in their_addr;<br>
	<br>
	UDTSOCKET recver = UDT::accept(serv, (sockaddr*)&amp;their_addr, &amp;namelen);<br>
	<br>
	char ip[16];<br>
	cout &lt;&lt; &quot;new connection: &quot; &lt;&lt; inet_ntoa(their_addr.sin_addr) &lt;&lt; &quot;:&quot; &lt;&lt; 
	ntohs(their_addr.sin_port) &lt;&lt; endl;<br>
	<br>
	char data[100];<br>
	<br>
	if (UDT::ERROR == UDT::recv(recver, data, 100, 0))<br>
	{<br>
&nbsp;&nbsp; cout &lt;&lt; &quot;recv:&quot; &lt;&lt; UDT::getlasterror().getErrorMessage() &lt;&lt; endl;<br>
&nbsp;&nbsp; return 0;<br>
	}<br>
	<br>
	cout &lt;&lt; data &lt;&lt; endl;<br>
	<br>
	CUDT::close(recver);<br>
	CUDT::close(serv);<br>
	<br>
	return 1;<br>
	}</font></td>
  </tr>
</table>
<p><font face="Verdana" size="2">This simple server tries to bind itself at port 
9000. If succeed, it listens at port 9000 and accepts a client and then reads a string.
</font></p>
<p style="margin-top: 0; margin-bottom: 0"><b><font face="Verdana" size="2">UDT 
client example</font></b></p>
<p style="margin-top: 0; margin-bottom: 0">&nbsp;</p>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="100%" id="table3" bgcolor="#C0C0C0" bordercolor="#C0C0C0">
  <tr>
    <td width="100%">
    <p style="margin-top: 0; margin-bottom: 0">
	<font face="Courier New" size="1">#include &lt;iostream&gt;<br>
	#include &lt;udt.h&gt;<br>
	#include &lt;arpa/inet.h&gt;<br>
	<br>
	using namespace std;<br>
	using namespace UDT;<br>
	<br>
	int main()<br>
	{<br>
	UDTSOCKET client = UDT::socket(AF_INET, SOCK_STREAM, 0);<br>
	<br>
	sockaddr_in serv_addr;<br>
	serv_addr.sin_family = AF_INET;<br>
	serv_addr.sin_port = htons(9000);<br>
	inet_pton(AF_INET, &quot;127.0.0.1&quot;, &amp;serv_addr.sin_addr);<br>
	<br>
	memset(&amp;(serv_addr.sin_zero), '\0', 8);<br>
	<br>
	// connect to the server, implict bind<br>
	if (UDT::ERROR == UDT::connect(client, (sockaddr*)&amp;serv_addr, 
	sizeof(serv_addr)))<br>
	{<br>
&nbsp;&nbsp; cout &lt;&lt; &quot;connect: &quot; &lt;&lt; UDT::getlasterror().getErrorMessage();<br>
&nbsp;&nbsp; return 0;<br>
	}<br>
	<br>
	char* hello = &quot;hello world!\n&quot;;<br>
	if (UDT::ERROR == UDT::send(client, hello, strlen(hello) + 1, 0))<br>
	{<br>
&nbsp;&nbsp; cout &lt;&lt; &quot;send: &quot; &lt;&lt; UDT::getlasterror().getErrorMessage();<br>
&nbsp;&nbsp; return 0;<br>
	}<br>
	<br>
	UDT::close(client);<br>
	<br>
	return 1;<br>
	}</font></td>
  </tr>
</table>
<p><font face="Verdana" size="2">The client side connects to the local address 
(127.0.0.1) at port 9000, and sends a &quot;hello world!&quot; message.</font></p>

</body>

</html>